package cn.lyjuan.citycode.pinyin.manager;

import bean.CityCodeBean;
import cn.lyjuan.citycode.pinyin.thread.PinyinRunnable;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PinyinManager
{
    private PinyinManager()
    {
    }
    private static PinyinManager obj;
    public static PinyinManager getInstance()
    {
        if (null == obj)
        {
            synchronized (PinyinManager.class)
            {
                if (null == obj)
                    obj = new PinyinManager();
            }
        }
        return obj;
    }

    public void covert(List<CityCodeBean> list)
    {
        for (CityCodeBean b : list)
            run(new PinyinRunnable(this, b));

//        es.shutdown();
//
//        while (true)
//        {
//            if (!es.isTerminated())
//                waitThrow();
//
//            if (es.isTerminated())
//                break;
//        }

        System.out.println("all covert compiled!");
    }

    public void notifyThrow()
    {
        synchronized (obj)
        {
            this.notifyAll();
        }
    }

    private void waitThrow()
    {
        try
        {
            synchronized (obj)
            {
                this.wait();
            }
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    private ExecutorService es;

    private void run(Runnable run)
    {
        run.run();

//        if (null == es)
//            es = Executors.newFixedThreadPool(2);
//
//        es.execute(run);
    }
}
